package cn.iocoder.yudao.module.erp.dal.mysql.purchaseorder;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.framework.mybatis.core.query.QueryWrapperX;
import cn.iocoder.yudao.module.erp.controller.admin.purchaseorder.vo.PurchaseOrderPageReqVO;
import cn.iocoder.yudao.module.erp.dal.dataobject.purchaseorder.PurchaseOrderDO;
import org.apache.ibatis.annotations.Mapper;

/**
 * 采购订单 Mapper
 *
 * @author maxuxu
 */
@Mapper
public interface PurchaseOrderMapper extends BaseMapperX<PurchaseOrderDO> {

    default PageResult<PurchaseOrderDO> selectPage(PurchaseOrderPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<PurchaseOrderDO>()
                .eqIfPresent(PurchaseOrderDO::getBillNo, reqVO.getBillNo())
                .eqIfPresent(PurchaseOrderDO::getBillContacter, reqVO.getBillContacter())
                .likeIfPresent(PurchaseOrderDO::getBillContacterName, reqVO.getBillContacterName())
                .eqIfPresent(PurchaseOrderDO::getBillContacterMobile, reqVO.getBillContacterMobile())
                .eqIfPresent(PurchaseOrderDO::getWarehouseId, reqVO.getWarehouseId())
                .likeIfPresent(PurchaseOrderDO::getWarehouseName, reqVO.getWarehouseName())
                .eqIfPresent(PurchaseOrderDO::getSupplierOrderNo, reqVO.getSupplierOrderNo())
                .eqIfPresent(PurchaseOrderDO::getSupplierId, reqVO.getSupplierId())
                .likeIfPresent(PurchaseOrderDO::getSupplierName, reqVO.getSupplierName())
                .likeIfPresent(PurchaseOrderDO::getSupplierContacterName, reqVO.getSupplierContacterName())
                .eqIfPresent(PurchaseOrderDO::getSupplierMobile, reqVO.getSupplierMobile())
                .eqIfPresent(PurchaseOrderDO::getAggregateQuantity, reqVO.getAggregateQuantity())
                .eqIfPresent(PurchaseOrderDO::getAggregateAmount, reqVO.getAggregateAmount())
                .eqIfPresent(PurchaseOrderDO::getDiscountAmount, reqVO.getDiscountAmount())
                .eqIfPresent(PurchaseOrderDO::getTotalAmount, reqVO.getTotalAmount())
                .eqIfPresent(PurchaseOrderDO::getDeliveryAddress, reqVO.getDeliveryAddress())
                .likeIfPresent(PurchaseOrderDO::getGoodsInfo, reqVO.getGoodsInfo())
                .eqIfPresent(PurchaseOrderDO::getProjectId, reqVO.getProjectId())
                .likeIfPresent(PurchaseOrderDO::getProjectName, reqVO.getProjectName())
                .eqIfPresent(PurchaseOrderDO::getSettlementId, reqVO.getSettlementId())
                .likeIfPresent(PurchaseOrderDO::getSettlementName, reqVO.getSettlementName())
                .eqIfPresent(PurchaseOrderDO::getRemarks, reqVO.getRemarks())
                .eqIfPresent(PurchaseOrderDO::getFile, reqVO.getFile())
                .eqIfPresent(PurchaseOrderDO::getSyncPay, reqVO.getSyncPay())
                .eqIfPresent(PurchaseOrderDO::getSyncIn, reqVO.getSyncIn())
                .eqIfPresent(PurchaseOrderDO::getSyncInvoice, reqVO.getSyncInvoice())
                .betweenIfPresent(PurchaseOrderDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(PurchaseOrderDO::getId));
    }

    default PurchaseOrderDO selectByNo(String no) {
        return selectOne(PurchaseOrderDO::getBillNo, no);
    }


    default PurchaseOrderDO getSumOrderAmount() {
        QueryWrapperX<PurchaseOrderDO> wrapperX = new QueryWrapperX<>();
        wrapperX.select("SUM(total_amount) as totalAmount");
        return selectOne(wrapperX);
    }

    default PurchaseOrderDO getSumOrderAmountBySyncPay(Integer syncPay) {
        QueryWrapperX<PurchaseOrderDO> wrapperX = new QueryWrapperX<>();
        wrapperX.select("SUM(total_amount) as totalAmount")
                .eq("sync_pay", syncPay);
        return selectOne(wrapperX);
    }

}